/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file telemPages.h

  \author Cory Dixon
*/

#ifndef __TELEM_PAGE_H
#define __TELEM_PAGE_H

#include "NodePages.h"


class TelemPage : public NodePage
{
	public:
		~TelemPage();
		virtual void update();
		virtual bool create_page(Client *client, GtkBuilder *builder);
		virtual void setCMD_RX(uint8_t cmd_id, int ack_nack=-1);

	protected:
		//--------------------PAGE VARIABLES--------------------//
	
		//-- display --//
		//   in this section, add the names of the widgets you designed
		//   in glade for the page you are creating
		GtkWidget *gps_lat;
		GtkWidget *gps_lon;
		GtkWidget *gps_heading;
		GtkWidget *gps_height;
		GtkWidget *gps_speed;
		GtkWidget *gps_pdop;
		GtkWidget *gps_sat;
		GtkWidget *current_wp;
		GtkWidget *ad_tas;
		GtkWidget *ad_alt;

		GtkWidget *gps_agl;

		GtkWidget *rpm_stat;
		GtkWidget *alt_stat;
		GtkWidget *ias_stat;
		GtkWidget *att_stat;
		GtkWidget *sys_stat;
		GtkWidget *gps_stat;
		GtkWidget *link_stat;
		GtkWidget *com_stat;
		GtkWidget *term_stat;
		GtkWidget *engine_stat;
		GtkWidget *deadman_stat;
		GtkWidget *main_v;
		GtkWidget *main_ma;
		GtkWidget *servo_v;
		GtkWidget *servo_ma;

		GtkWidget *flight_time;
		bool timer_running;
		
		//-- input entry --//
		//  in this section, add any input boxes you gave to the user
		//  for input selection, can be buttons, text input, check boxes, ...
		
		GtkWidget *goto_wp;   // input a waypoint to goto
		GtkWidget *wp_send;   // send the selected waypoint

		GtkWidget *timer_ss;    // timer stop-start
		GtkWidget *timer_reset; // timer reset
		float     timer_val;

		GtkWidget *gs_back_ip;
		GtkWidget *gs_back_port;
		GtkWidget *gs_back_serial;
		GtkWidget *gs_back_connect;

		GtkWidget *gs_back_telem;
		GtkWidget *gs_back_fp;
		GtkWidget *gs_back_wind;

		GtkWidget *rssi;
		GtkWidget *throttle;
		GtkWidget *loop_ias_telem;
		GtkWidget *loop_ias;
		GtkWidget *goto_ias;
		GtkWidget *loop_ias_send;
		GtkWidget *loop_alt_telem;
		GtkWidget *loop_alt;
		GtkWidget *goto_alt;
		GtkWidget *loop_alt_send;

		GtkWidget *wp_off;
		GtkWidget *wp_on;
		GtkWidget *ias_off;
		GtkWidget *ias_on;
		GtkWidget *ias_auto;
		GtkWidget *alt_off;
		GtkWidget *alt_on;
		GtkWidget *alt_auto;

		GtkWidget *batt_ah;
		GtkWidget *batt_amps;
		GtkWidget *batt_ah_reset;
		float battery_amps;
		float battery_amp_hours;

		bool loop_init;

		//-- callbacks --//
		//  in this section, add any of the callbacks you need to handle
		//  user input. Note, more than likely these will need to be static functions.
		//  Also, for the pointer we pass the WindowStruct pointer, which provides
		//  all the inormatio necessary to dray any/all pages
		
		// send the waypoint value to goto
		static void wp_send_clicked(GtkButton *widget, gpointer data); 
		static void loop_ias_send_clicked(GtkButton *widget, gpointer data); 
		static void loop_alt_send_clicked(GtkButton *widget, gpointer data); 
		// limit what the user can input into wp input
		static gboolean goto_wp_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data); // 
		// do somthing useful
		static void timer_ss_toggle(GtkButton *widget, gpointer data); 
		static void timer_reset_clicked(GtkButton *widget, gpointer data); 
		static void gs_back_connect_clicked(GtkButton *widget, gpointer data); 
		void set_gs_back(void); 
		void connect_to_gs(const gchar * ipStr, const gchar * portStr);

		static void batt_ah_reset_clicked(GtkButton *widget, gpointer data); 
		TimeStamp last_ah_estimate;
		void estimate_ah(float throttle, bool engine_enabled);
		float throttle_lookup(float throttle);
		void reset_batt_stats();

		GtkWidget *eta_estimate;
		GtkWidget *headwind_field;
		void estimate_eta(float ias, float bearing, float distance, float wind_n, float wind_e);
		void reset_eta();
		float ias_avg;
		float wind_n_avg;
		float wind_e_avg;
		float eta;
		float headwind;

		GtkWidget *return_eta_field;
		void estimate_turn_eta();
		float return_eta;
		float average_amps;
		long amp_samples;

		TimeStamp flight_timer_ts;
		long timer_minutes;
};

#endif
